package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.enums.param.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.activity.ActivityDTO;
import com.qk.entity.Activity;
import com.qk.entity.Clue;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ClueMapper clueMapper;

    @Override
    public Result getActivityList(ActivityDTO activityDTO) {
        boolean hasNull = BeanUtil.hasNullField(activityDTO);
        if (hasNull) {
            CommonException.throwException(ParamEnum.PARAM_ERROR);
        }
        PageHelper.startPage(activityDTO.getPage(), activityDTO.getPageSize());
        List<Activity> activityList = activityMapper.selectActivityByPage(activityDTO);
        Page<Activity> page = (Page<Activity>) activityList;
        PageResult<Activity> pageResult = PageResult.<Activity>builder()
                .total(Math.toIntExact(page.getTotal()))
                .rows(page.getResult())
                .build();
        return Result.success(pageResult);
    }

    @Override
    public Result getActivityById(Integer id) {
        Activity activity = activityMapper.selectActivityById(id);
        return Result.success(activity);
    }

    @Override
    public Result getActivityByType(Integer type) {
        List<Activity> activityList = activityMapper.selectActivityByType(type);
        return Result.success(activityList);
    }

    @Override
    public Result addActivity(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity);
        if (hasNull) {
            CommonException.throwException(ParamEnum.PARAM_ERROR);
        }
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insertActivity(activity);
        return Result.success();
    }

    @Override
    public Result deleteActivity(Integer id) {
        List<Clue> clueList = clueMapper.selectList(Wrappers.lambdaQuery(Clue.class).eq(Clue::getActivityId, id));
        if (!clueList.isEmpty()) {
            CommonException.throwException(ParamEnum.ACTIVITY_REFERENCE_ERROR);
        }
        activityMapper.deleteActivity(id);
        return Result.success();
    }

    @Override
    public Result updateActivity(Activity activity) {

        activityMapper.updateActivity(activity);
        return Result.success();
    }
}
